define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var table = null;

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jy/product/index' + location.search,
                    add_url: 'jy/product/add',
                    edit_url: 'jy/product/edit',
                    del_url: 'jy/product/del',
                    multi_url: 'jy/product/multi',
                    import_url: 'jy/product/import',
                    table: 'product',
                }
            });

            table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'update_time',
                search: false,
                undefinedText: '',
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                    ignoreColumn: ['operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var sort = params.sort;
                    if (filter.is_print) {
                        if (filter.is_print === '1') {
                            filter['print_num'] = 0;
                            op['print_num'] = '>';
                        } else {
                            filter['print_num'] = 0;
                            op['print_num'] = '=';
                        }
                        delete filter['is_print'];
                        delete op['is_print'];
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {
                            field: 'cover_images',
                            title: __('Cover_images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {field: 'product_name', title: __('Product_name'), operate: 'LIKE'},
                        {field: 'place', title: __('place'), operate: 'LIKE'},
                        // {field: 'series.series_name', title: __('Series_id'), operate: 'LIKE'},
                        // {field: 'color.color_name', title: __('Color_id'), operate: 'LIKE'},
                        {field: 'color', title: __('Color'), operate: 'LIKE'},
                        {field: 'surface', title: __('Surface'), operate: 'LIKE'},
                        {field: 'lines', title: __('Lines'), operate: 'LIKE'},
                        {field: 'specs', title: __('Specs'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), sortable: true, operate: false},
                        {
                            field: 'is_print',
                            title: __('Is_print'),
                            sortable: false,
                            visible: false,
                            searchList: {0: __('Is_print_0'), 1: __('Is_print_1')}
                        },
                        {
                            field: 'print_num',
                            title: __('Print_num'),
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"1": __('Status 1'), "0": __('Status 0')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                        },
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'qrcode',
                                    text: '二维码',
                                    classname: 'btn btn-xs btn-dialog btn-info',
                                    extend: 'data-area=["300px","300px"]',
                                    url: function (value) {
                                        return 'jy/product/qrcode?ids=' + value.id;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("fa.event.appendfieldlist", ".btn-append", function (e, obj) {
                Form.events.plupload(obj);
                Form.events.faselect(obj);
                Form.events.selectpicker(obj);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", ".btn-append", function (e, obj) {
                Form.events.plupload(obj);
                Form.events.faselect(obj);
                Form.events.selectpicker(obj);
            });
            Controller.api.bindevent();
        },
        qrcode: function () {
            Controller.api.bindevent();
            $('.btn-print').click(function () {
                Fast.api.ajax({
                    url: 'jy/product/addPrintNum?ids=' + Config.productId,
                }, function (data, ret) {
                    parent.$(".btn-refresh").trigger("click");
                    return false; // 关闭成功的提示
                });
                window.print();
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});